package edu.pe.cayetano.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.beanutils.DynaBean;

import ws.synopsis.frmwrk.common.BResult;
import edu.pe.cayetano.framework.Configuration;
import edu.pe.cayetano.framework.DAOException;
import edu.pe.cayetano.model.to.FichaExtraOralTO;
import edu.pe.cayetano.seccd.constantes.ConstantesSECCD;
import edu.pe.cayetano.util.Utilitario;

public class FichaExtraOralDAO  extends BaseDAO{
	
	String 					schema			= Configuration.getSchemaDB();
	
	public FichaExtraOralDAO(Connection cnx) {
		super(cnx);
	}

	public BResult buscarFichaExtraOralEspecifica(String codigo) throws Exception {

		FichaExtraOralTO 			fichaExtraTO 	= null;
		PreparedStatement   		pstmt 		 	= null;
		ResultSet 					rs 			 	= null;
		BResult						ret				= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" SELECT");
			query.append(" FILEHC,FORMADECRANEO,FORMADECARA,SIMETRIAFACIAL,MUSCULATURA,PERFIL_AP,PERFIL_VERTICAL,");
			query.append(" GLANDULAS_SALIVALES,FONACION,DEGLUCION,RESPIRACION,OTROS_HABITOS,ATM,");
			query.append(" MUSCULOS_MASTICATORIOS,APERTURA_BUCAL,GANGLIOS");
			query.append(" FROM "+schema+"FICHA_EXTRAORAL"); 
			query.append(" WHERE FILEHC=?");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,codigo.substring(2, codigo.length()));
			rs=pstmt.executeQuery();
			ret = new BResult();
			if(rs.next()){
				fichaExtraTO=new FichaExtraOralTO();
				fichaExtraTO.setFileHC(rs.getString(1));
				fichaExtraTO.setFormaDeCraneo(rs.getString(2));
				fichaExtraTO.setFormaDeCara(rs.getString(3));
				fichaExtraTO.setSimetriaFacial(rs.getString(4));
				fichaExtraTO.setMusculatura(rs.getString(5));
				fichaExtraTO.setPerfilAP(rs.getString(6));
				fichaExtraTO.setPerfilVertical(rs.getString(7));
				fichaExtraTO.setGlandulasSalivales(rs.getString(8));
				fichaExtraTO.setFonacion(rs.getString(9));
				fichaExtraTO.setDeglucion(rs.getString(10));
				fichaExtraTO.setRespiracion(rs.getString(11));
				fichaExtraTO.setOtrosHabitos(rs.getString(12));
				fichaExtraTO.setATM(rs.getString(13));
				fichaExtraTO.setMusculosMasticatorios(rs.getString(14));
				fichaExtraTO.setAperturaBucal(rs.getString(15));
				fichaExtraTO.setGanglios(rs.getString(16));
				ret.setObject(fichaExtraTO);
				ret.setCode(ConstantesSECCD.EXITO_RET);;
			}else{
				ret.setCode(1);
			}
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(ConstantesSECCD.ERROR_RET);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	}
	
	public BResult editaFichaExtraOralArchivoHC(DynaBean dyna) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" UPDATE "+schema+"FICHA_EXTRAORAL SET ");
			query.append(" FORMADECRANEO=?,FORMADECARA=?,SIMETRIAFACIAL=?,MUSCULATURA=?,PERFIL_AP=?,PERFIL_VERTICAL=?, ");
			query.append(" GLANDULAS_SALIVALES=?,FONACION=?,DEGLUCION=?,RESPIRACION=?,OTROS_HABITOS=?,ATM=?, ");
			query.append(" MUSCULOS_MASTICATORIOS=?,APERTURA_BUCAL=?,GANGLIOS=? ");
			query.append(" WHERE FILEHC=?"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,Utilitario.getPropertyDynaBean(dyna,"formaDeCraneo"));	 	
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"formaDeCara"));	 	
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"simetriaFacial"));	 	
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"musculatura"));	 	
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"perfilAP"));	 	
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"perfilVertical"));	 	
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"glandulasSalivales"));	
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"fonacion"));		
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"deglucion"));		
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"respiracion"));		
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"otrosHabitos"));		
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"ATM"));
			pstmt.setString(13,Utilitario.getPropertyDynaBean(dyna,"musculosMasticatorios"));	
			pstmt.setString(14,Utilitario.getPropertyDynaBean(dyna,"aperturaBucal"));	
			pstmt.setString(15,Utilitario.getPropertyDynaBean(dyna,"ganglios"));	
			pstmt.setString(16,filehc.substring(2,filehc.length()));
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(ConstantesSECCD.EXITO_RET);;
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(ConstantesSECCD.ERROR_RET);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 

	public BResult insertaFichaExtraOralArchivoHC(DynaBean dyna) throws Exception {

		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" INSERT INTO "+schema+"FICHA_EXTRAORAL VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,filehc.substring(2,filehc.length()));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"formaDeCraneo"));	 	
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"formaDeCara"));	 	
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"simetriaFacial"));	 	
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"musculatura"));	 	
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"perfilAP"));	 	
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"perfilVertical"));	 	
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"glandulasSalivales"));	
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"fonacion"));		
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"deglucion"));		
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"respiracion"));		
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"otrosHabitos"));		
			pstmt.setString(13,Utilitario.getPropertyDynaBean(dyna,"ATM"));	
			pstmt.setString(14,Utilitario.getPropertyDynaBean(dyna,"musculosMasticatorios"));	
			pstmt.setString(15,Utilitario.getPropertyDynaBean(dyna,"aperturaBucal"));	
			pstmt.setString(16,Utilitario.getPropertyDynaBean(dyna,"ganglios"));	
			pstmt.setInt(17,1);	
			pstmt.setString(18,"");	
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(ConstantesSECCD.EXITO_RET);;
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(ConstantesSECCD.ERROR_RET);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 
}
